﻿using pe.edu.pucp.lp2.controller;
using SalesModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SalesApp
{
    public partial class ProductoForm : Form
    {
        public ProductoForm()
        {
            InitializeComponent();
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int id = Int32.Parse(txtId.Text);
            String name = txtName.Text;
            String descripcion = txtDesc.Text;
            String ean = txtEan.Text;
            String brand = txtBrand.Text;
            int stock = Int32.Parse(txtStock.Text);
            double pu = Double.Parse(txtPu.Text);
            String categoria = cmbCategory.SelectedItem.ToString();


            Product c = new Product();

            c.setId_product(id);
            c.setName(name);
            c.setBrand(brand);
            c.setEan(ean);
            c.setBasePrice(pu);
            c.setDescription(descripcion);
            c.setStock(stock);
            c.setCategory(categoria);
            SalesManager.addProduct(c);
            RefreshGrid();
        }
        public void RefreshGrid()
        {
            dgvProducto.Rows.Clear();

            List<Product> list = SalesManager.getAllProducts();

            for (int i = 0; i < list.Count; i++)
            {
                dgvProducto.Rows.Add(new String[] { ""+list[i].getId_product(),
                                                         list[i].getName(),
                                                         list[i].getDescription(),
                                                         list[i].getEan(),
                                                         list[i].getBrand(),
                                                         "" + list[i].getBasePrice()
                });
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtId.Text = "";
            txtName.Text = "";
            txtDesc.Text = "";
            txtEan.Text = "";
            txtBrand.Text = "";
            txtStock.Text = "";
            txtPu.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int id = Int32.Parse(txtId.Text);
            SalesManager.deleteProduct(id);
            RefreshGrid();
        }

        private void ProductoForm_Load(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void dgvProducto_Click(object sender, EventArgs e)
        {
            int selRow = dgvProducto.SelectedRows[0].Index;
            int productId = Int32.Parse(dgvProducto.Rows[selRow].Cells[0].Value.ToString());
            Product c = SalesManager.getProductById(productId);

            txtId.Text = "" + c.getId_product();
            txtName.Text = c.getName();
            txtDesc.Text = c.getDescription();
            txtEan.Text = c.getEan();
            txtBrand.Text = c.getBrand();
            txtPu.Text = "" + c.getBasePrice();
            txtStock.Text = "" + c.getStock();
            cmbCategory.Text = c.getCategory();
        }
    }
}
